﻿// static的使用
/*
#include <iostream>
using namespace std;

class A
{
public:
    A(int a=1)
        :_a(a)
    {
        ++_n;
    }

    A(const A& aa)
    {
        ++_n;
    }
    static int GetN()                      // static成员函数没有this指针
    {
        return _n;
    }
private:
    int _a=0;
    static int _n;                             
    //这里的_n不属于某一个对象，而是整个类,所以要在类外面定义
};

int A::_n=0;                //_n在这里定义

A Func()
{
    A aa_;
    return aa_;
}

int main()
{
    A aa1;
    A aa2;
    A* aa_ptr = nullptr;
    Func();
    cout << aa1.GetN() << endl;
    cout << aa2.GetN() << endl;
    cout << aa_ptr->GetN() << endl;
    cout << A::GetN() << endl;
}

*/

//友元类
//友元类的所有成员函数都可以是另一个类的友元函数，都可以访问另一个类中的非公有成员。
/*
#include<iostream>
using namespace std;
class Time
{
    friend class Date;
public:
    Time(int hour=1,int minute=1,int second=1)
        :_hour()
        ,_minute(minute)
        ,_second(second)
    {}

    ~Time()
    {
        cout << "~Time()" << endl;
    }
private:
    int _hour;
    int _minute;
    int _second;

};

class Date
{
    friend class Time;
public:
    Date(int year=1,int month=1,int day=1)
        :_year(year)
        ,_month(month)
        ,_day(day)
    {}
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
    void SetTimeofDate(int hour,int minute,int second)
    {
        _t._hour = hour;
        _t._minute = minute;
        _t._second = second;
    }

    ~Date()
    {
        cout << "~A()" << endl;
    }

private:
    int _year;
    int _month;
    int _day;
    Time _t;
};

int main()
{
    Date a1;
    return 0;
}

*/


//内部类
/*
#include<iostream>
using namespace std;

class Date
{
public:
    class Time
    {
    public:
        Time(int hour = 1, int minute = 1, int second = 1)
            :_hour()
            , _minute(minute)
            , _second(second)
        {}

        ~Time()
        {
            cout << "~Time()" << endl;
        }
    private:
        int _hour;
        int _minute;
        int _second;

    };
    Date(int year = 1, int month = 1, int day = 1)
        :_year(year)
        , _month(month)
        , _day(day)
    {}
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

    ~Date()
    {
        cout << "~A()" << endl;
    }

private:
    int _year;
    int _month;
    int _day;
    Time _t;
};

int main()
{
    Date a1;
    return 0;
}

*/


//有名对象和匿名对象

/*
#include<iostream>
using namespace std;

class A
{
public:
    A(int year = 1, int month = 1, int day = 1)
        :_year(year)
        , _month(month)
        , _day(day)
    {
        cout << "A(int year = 1, int month = 1, int day = 1)" << endl;
    }
    void Print()const
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
    ~A()
    {
        cout << "~A()" << endl;
    }
private:
    int _year;
    int _month;
    int _day;
};
class Solution
{
public:
    Solution()
    {
        cout << "Solution()" << endl;
    }
    int Sum_Solution(int n)
    {
        cout << "int Sum_Solution()" << endl;
        return n;
    }
    ~Solution()
    {
        cout << "~Solution()" << endl;
    }
private:

};

int main()
{
    A aa1;                       //有名对象
    aa1.Print();

    A aa2(2024, 9, 26);         //有名对象
    aa2.Print();

    //匿名对象
    A();                        //调用一次构造+析构就结束了，
    A(2024, 5, 22);

    //只是想调用一次对象里的函数
    //传统方式：
    Solution s1;
    s1.Sum_Solution(10);

    //创建匿名对象方式
    Solution().Sum_Solution(10);

    return 0;
}

*/


//编译器优化拷贝构造,即构造+构造或者拷贝构造+拷贝构造会被合二为一优化处理
/*
#include<iostream>
using namespace std;

class A
{
public:
    A(int a= 1)
        :_a(a)

    {
        cout << "A(int year = 1, int month = 1, int day = 1)" << endl;
    } 
    A(const A& aa)                //拷贝构造
        :_a(aa._a)

    {
        cout << "A(const A& aa)" << endl;
    }
    ~A()
    {
        cout << "~A()" << endl;
    }
private:
    int _a;
};

void fun1(A aa)
{
    //.....
}


void fun2(const A& aa)
{
    //.....
}

int main()
{
    A aa1=2;               //先构造再拷贝构造   -》优化成直接构造         
    const A& aa2 = 2;      //没办法拷贝构造，只有构造；引用的是2产生的临时变量，具有常性，加const修饰

    A aa3(6);              //
    fun1(aa3);             //对象aa3传给aa需要拷贝构造，
    fun1(A(aa3));          //先构造再拷贝构造，然后会被编译器优化成直接构造

    //如果不想拷贝构造发生可以传引用
    fun2(aa3);
    return 0;
}
*/

